<template>
  <div>
    <PublicHead
        :label="'策略名'"
        :add="add"
        :on-search="onSearch"
    ></PublicHead>
    <a-table
        rowKey="objectId"
        size="middle"
        :pagination="currentPagination"
        :columns="columns"
        :data-source="this.showTableDada"
        @change="pageChange"
    >
      <span slot="action" slot-scope="text">

        <a @click="handleEdit(text.objectId)">编辑</a>
        <a-divider type="vertical"/>
        <a @click="manageRule(text.objectId)">管理规则</a>
        <a-divider type="vertical"/>
        <a-popconfirm
            title="确定删除吗？"
            @confirm="confirm(text.objectId)"
        >
          <a-icon slot="icon" type="question-circle-o" style="color: red"/>
          <a>删除</a>
        </a-popconfirm>
        <a-divider type="vertical"/>
        <a-switch
            checked-children="开"
            un-checked-children="关"
            :checked="text.status?true:false"
            @click="onChange(text.objectId,text.status)"
        />
      </span>
    </a-table>
    <ActivationPopup></ActivationPopup>
    <ManageRule></ManageRule>
  </div>
</template>

<script>
import PublicHead from "@/components/share/PublicHead";
import {mapActions, mapMutations, mapState} from "vuex";
import {find} from "lodash";
import ActivationPopup from "@/pages/systemManage/activation/ActivationPopup";
import ManageRule from "@/pages/systemManage/activation/manageRule/ManageRule";

export default {
  name: "Activation",
  components: {ManageRule, ActivationPopup, PublicHead},
  created() {
    let {objectId} = this.$router.currentRoute.params
    this.setState({name: 'currentKey', value: objectId})
    this.queryAllActivation()
  },
  data() {
    return {
      currentPagination: {current: 1},
      columns: [
        {
          title: '序号',
          align: 'center',
          customRender: (text, record, index) => {

            return (this.currentPagination.current - 1) * 10 + index + 1
          },
          width: 100
        }, {
          title: '策略名',
          dataIndex: 'label',
          key: 'label',
          align: 'center',
        }, {
          title: '备注',
          dataIndex: 'comment',
          key: 'comment',
          align: 'center',
        }, {
          title: '警戒值',
          dataIndex: 'median',
          key: 'median',
          align: 'center',
        }, {
          title: '拒绝值',
          dataIndex: 'high',
          key: 'high',
          align: 'center',
        }, {
          title: '操作',
          key: 'action',
          scopedSlots: {customRender: 'action'},
          align: "center",
          width: 300
        }
      ]
    }
  },
  computed: {
    ...mapState('activation', ['currentKey','tableData', 'popupVisible', 'showTableDada'])
  },
  methods: {
    ...mapMutations('activation', ['setState']),
    ...mapMutations('rule', {setRuleState:'setState'}),
    ...mapActions('activation', ['queryAllActivation', 'deleteById', 'update', 'disable']),
    ...mapActions('rule', ['queryAllRule']),
    pageChange(obj) {
      this.currentPagination = obj
    },
    add() {
      this.setState({name: 'editData', value: null})
      this.setState({name: 'popupTitle', value: '添加'})
      this.setState({name: 'popupVisible', value: true})
    },
    onSearch(val) {
      let list = []
      if (val) {
        for (let tableDatum of this.tableData) {
          if (tableDatum.label.indexOf(val) >= 0) {
            list.push(tableDatum)
          }
        }
      } else {
        list = this.tableData
      }
      this.setState({name: 'showTableDada', value: list})
    },
    handleEdit(key) {
      this.setState({name: 'editData', value: find(this.tableData, {objectId: key})})
      this.setState({name: 'currentEditKey', value: key})
      this.setState({name: 'popupTitle', value: '编辑'})
      this.setState({name: 'popupVisible', value: true})
    },
    confirm(key) {
      this.deleteById(key).then(() => {
        this.queryAllActivation()
      })
    },
    onChange(objectId, status) {
      this.disable({objectId, status})
    },
    manageRule(key) {
      this.setState({name: 'managePopupVisible', value: true})
      this.setRuleState({name: 'currentKey', value: this.currentKey})
      this.setRuleState({name: 'currentActivationKey', value: key})
      this.queryAllRule()
    }
  }
}
</script>

<style scoped>

</style>
